Tranformation of the data files extracted for AP district treasuries to the upload format we want for OpenBudgetsIndia

The format we want is :-

 |-- State_District
 |  | -- state_district_month_year.csv

In [1]:
import pandas as pd
import os
import csv

In [2]:
!ls ../csvs/ditrict_treasuries/ap/2016-17


EASTGODAVARI.csv  SRIKAKULAM.csv     WESTGODAVARI.csv
krishna.csv	  VISAKHAPATNAM.csv

In [11]:
def transform_and_create(filepath, year):
    data = pd.read_csv(filepath)
    district_name = filepath.split('/')[-1].split('.')[0]
    base_dir = '../csvs/ditrict_treasuries/andhra_pradesh/' + district_name
    data['fin_year'] = year
    if not os.path.isdir(base_dir):
        os.makedirs(base_dir)
    for month, group in data.groupby('MONTH'):
        new_filename = 'andhra_pradesh_{2}_{0}_{1}.csv'.format(month, year, district_name)
        new_filepath = os.path.join(base_dir, new_filename)
        group.to_csv(new_filepath, index=False, quoting=csv.QUOTE_ALL)
    return True

In [13]:
csv_dir = '../csvs/ditrict_treasuries/ap/2016-17'
for filename in os.listdir(csv_dir):
    filename = os.path.join(csv_dir, filename)
    transform_and_create(filename, '2016-17')

In [14]:
csv_dir = '../csvs/ditrict_treasuries/ap/2017-18'
for filename in os.listdir(csv_dir):
    filename = os.path.join(csv_dir, filename)
    transform_and_create(filename, '2017-18')

In [ ]: